package generic;

import java.util.ArrayList;

public class GenericMethodTest<T> {
    private T t;
    private Class<T> kind;

    public static <T> T func1(T t) {
        System.out.println("f1() the t's type is " + t.getClass().getName());
        return t;
    }

    public static <T> void func2(T t) {
        System.out.println("func2() t is " + t.toString());
    }

    public void setKind(Class<T> k) {
        kind = k;
    }

    public void check(Object obj) {
        System.out.println("");
        System.out.println("check() ***************************");
//        System.out.println("func3() obj is the type of t: "  + (obj instanceof T));
        System.out.println("func3() check is the type of t: " + (obj instanceof ArrayList));
//        T test = new T();
//        T[] array = new T[10];
        T test = (T) obj;
        System.out.println("check() test type is " + test.getClass().getName());
//        T arrayTest = (T[]) new Object[2];

        if (kind != null) {
            System.out.println("check() obj's type is the same with kind " + kind.isInstance(obj));
        }
    }

    public static void main(String[] args) {
        Object result = func1("String");
        Object integer = func1(2222);
        Object doubleValue = func1(0.0F);
        Object characterValue = func1('C');
        GenericMethodTest test2 = func1(new GenericMethodTest());

        func2("666");

        GenericMethodTest genericMethodTest = new GenericMethodTest<GenericMethodTest2>();
        genericMethodTest.setKind(((new GenericMethodTest2()).getClass()));
        genericMethodTest.check(new GenericMethodTest2());
        genericMethodTest.check(new GenericMethodTest());
        genericMethodTest.check("2233");


        GenericMethodTest2 genericMethodTest2 = new GenericMethodTest2();
        System.out.println("GenericMethodTest2 type is " + ((GenericMethodTest2) genericMethodTest2).getClass().getName());
    }
}

class GenericMethodTest2 extends GenericMethodTest {
}
